<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div></div>
    <script>
        const div = document.querySelector('div')
        function fun() {
            const date = new Date()
            //使时间补零
            let h = date.getHours()
            let m = date.getMinutes()
            let s = date.getSeconds()
            h = h < 10 ? '0' + h : h
            return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}-${h}:${m}:${s} 
        `//如果是写document.write 就会一直打印下去
        }
        div.innerHTML = fun()

        //要使其隔一秒来进行刷新
        setInterval(function () {
            div.innerHTML = fun()
        }, 1000)

    </script>
</body>

</html>